package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sangeng.entity.GlcUser;
import com.sangeng.entity.User;
import com.sangeng.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class GlcUserDetailsServiceImpl implements UserDetailsService {

     @Autowired
     private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getUserName, name);

        User user = userMapper.selectOne(wrapper);
        if (user == null){
            throw  new UsernameNotFoundException("用户名或用户不存在");
        }

        //TODO 查询角色 授权




        //封装成UserDetails
        return new GlcUser(user);
    }




}
